package com.gj.client;

import io.netty.buffer.ByteBuf;

/**
 * @author: xiaobin
 * @create: 2020-12-14 23:08
 */
public class Test {
    public static void main(String[] args) {
//
//        int c = 0xaa;
//        System.out.println(c);
//        System.out.println(Integer.toBinaryString(c));
//
//        byte a = (byte)0xaa;
//        System.out.println((byte)0xaa);
//        System.out.println(Byte.toUnsignedInt(a));

//        System.out.println(intTohex(-86));

        System.out.println((byte)0x00);

        byte aa = (byte) 0xAA;
        System.out.println(aa);

        System.out.println(byteToHex(aa));

    }

    /**
     * 字节转十六进制
     * @param b 需要进行转换的byte字节
     * @return  转换后的Hex字符串
     */
    public static String byteToHex(byte b){
        String hex = Integer.toHexString(b & 0xFF);
        if(hex.length() < 2){
            hex = "0" + hex;
        }
        return hex;
    }

    //数据和
    public static byte  getDataAdd(byte[] bs){
        byte he = 0;
        if(bs != null){
            for (int i = 0; i < bs.length; i++) {
                he += bs[i];
            }
        }
        return he;
    }

    private static int indexOf(ByteBuf haystack, byte b) {
        for (int i = 0; i < haystack.writerIndex(); i ++) {
            if(haystack.getByte(i) == b){
                int index = i+haystack.getByte(i+1)+3;
                int he = haystack.getByte(index-1);
                byte[] hh = new byte[index-i-2];
                haystack.getBytes(i+1, hh, 0, hh.length);
                if(he == getDataAdd(hh)){
                    return index;
                }
            }
        }
        return -1;
    }



    public static String intTohex(int n) {
        StringBuffer s = new StringBuffer();
        String a;
        char[] b = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        while (n != 0) {
            s = s.append(b[n % 16]);
            n = n / 16;
        }
        a = s.reverse().toString();
        if ("".equals(a)) {
            a = "00";
        }
        if (a.length() == 1) {
            a = "0" + a;
        }
        return a;
    }
}
